﻿using System;
using Assets.Scripts.GtsScripts;
using RootMotion.FinalIK;
using UnityEngine;

// Token: 0x020002CA RID: 714
public class HeadIK
{
	// Token: 0x060010A5 RID: 4261 RVA: 0x00079FF0 File Offset: 0x000781F0
	public HeadIK(LookAtIK ik, Giantess giantess)
	{
		this.ik = ik;
		this.giantess = giantess;
		this.head = giantess.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.Head);
		this.neck = giantess.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.Neck);
		this.SetDefaultValues();
	}

	// Token: 0x17000265 RID: 613
	// (get) Token: 0x060010A6 RID: 4262 RVA: 0x0007A05E File Offset: 0x0007825E
	// (set) Token: 0x060010A7 RID: 4263 RVA: 0x0007A06B File Offset: 0x0007826B
	private Vector3 targetPosition
	{
		get
		{
			return CenterOrigin.VirtualToWorld(this.virtualTargetPosition);
		}
		set
		{
			this.virtualTargetPosition = CenterOrigin.WorldToVirtual(value);
		}
	}

	// Token: 0x17000266 RID: 614
	// (get) Token: 0x060010A8 RID: 4264 RVA: 0x0007A079 File Offset: 0x00078279
	// (set) Token: 0x060010A9 RID: 4265 RVA: 0x0007A086 File Offset: 0x00078286
	private Vector3 ikPosition
	{
		get
		{
			return CenterOrigin.VirtualToWorld(this.virtualIKPosition);
		}
		set
		{
			this.virtualIKPosition = CenterOrigin.WorldToVirtual(value);
		}
	}

	// Token: 0x060010AA RID: 4266 RVA: 0x0007A094 File Offset: 0x00078294
	public void SetDefaultValues()
	{
		this.ik.enabled = true;
		this.ik.fixTransforms = false;
		this.ik.solver.bodyWeight = this.bodyWeight;
		this.ik.solver.headWeight = 1f;
		this.ik.solver.eyesWeight = 0.4f;
		this.ik.solver.clampWeight = 0.5f;
		this.ik.solver.clampWeightHead = 0.6f;
		this.ik.solver.clampWeightEyes = 0.7f;
		this.ik.solver.clampSmoothing = 2;
		this.CurrentState = new HeadIK.State(this.Start);
	}

	// Token: 0x060010AB RID: 4267 RVA: 0x0007A15B File Offset: 0x0007835B
	public void LookAt(EntityBase entity)
	{
		this.target = entity;
		this.CurrentState = new HeadIK.State(this.Look);
	}

	// Token: 0x060010AC RID: 4268 RVA: 0x0007A176 File Offset: 0x00078376
	public void DisableLookAt()
	{
		this.CurrentState = new HeadIK.State(this.TotalDisable);
	}

	// Token: 0x060010AD RID: 4269 RVA: 0x0007A18A File Offset: 0x0007838A
	public void LookAtPoint(Vector3 point)
	{
		if (!GlobalPreferences.LookAtPlayer.value || !this.giantess.canLookAtPlayer)
		{
			return;
		}
		this.lookDirection = point;
		this.CurrentState = new HeadIK.State(this.LookPoint);
	}

	// Token: 0x060010AE RID: 4270 RVA: 0x0007A1C5 File Offset: 0x000783C5
	public void Cancel()
	{
		this.target = null;
		this.CurrentState = new HeadIK.State(this.Start);
	}

	// Token: 0x060010AF RID: 4271 RVA: 0x0007A1E0 File Offset: 0x000783E0
	public void Update()
	{
		this.CurrentState();
		this.UpdateEffector();
	}

	// Token: 0x060010B0 RID: 4272 RVA: 0x0007A1F4 File Offset: 0x000783F4
	private void UpdateEffector()
	{
		this.speed = this.giantess.animationManager.GetCurrentSpeed();
		float magnitude = (this.giantess.transform.InverseTransformPoint(this.head.position) - this.giantess.transform.InverseTransformPoint(this.targetPosition)).magnitude;
		this.ik.solver.headWeight = Mathf.Clamp01(magnitude / this.headDistance) * 1f;
		if (this.neck.position.y < this.targetPosition.y)
		{
			if (this.bodyWeight > 0.2f)
			{
				this.bodyWeight = Mathf.Lerp(this.bodyWeight, 0.2f, Time.fixedDeltaTime * this.speed);
			}
		}
		else if (this.bodyWeight < 0.75f)
		{
			this.bodyWeight = Mathf.Lerp(this.bodyWeight, 0.75f, Time.fixedDeltaTime * this.speed);
		}
		this.ik.solver.bodyWeight = Mathf.Clamp01(magnitude / this.bodyDistance) * this.bodyWeight;
		this.ik.solver.IKPositionWeight = Mathf.Lerp(this.ik.solver.IKPositionWeight, this.targetWeight, Time.fixedDeltaTime * this.speed);
		this.ik.solver.IKPosition = Vector3.Slerp(this.ikPosition, this.targetPosition, Time.fixedDeltaTime * 4f * this.speed);
		this.ikPosition = this.ik.solver.IKPosition;
	}

	// Token: 0x060010B1 RID: 4273 RVA: 0x0007A3AC File Offset: 0x000785AC
	private void Start()
	{
		if (this.target == null || this.target.isDead)
		{
			this.target = GameController.playerInstance;
		}
		this.targetWeight = Mathf.Lerp(this.targetWeight, 0f, Time.deltaTime * this.speed);
		if (this.giantess.senses.CheckVisibility(this.target))
		{
			this.CurrentState = new HeadIK.State(this.Look);
		}
	}

	// Token: 0x060010B2 RID: 4274 RVA: 0x0007A434 File Offset: 0x00078634
	private void CantSee()
	{
		this.bodyWeight = Mathf.Lerp(this.bodyWeight, 0.2f, Time.fixedDeltaTime * this.speed);
		this.targetWeight = Mathf.Lerp(this.targetWeight, 0.1f, Time.deltaTime * this.speed * 0.2f);
		if (this.target != null && this.giantess.senses.CheckVisibility(this.target))
		{
			this.CurrentState = new HeadIK.State(this.Look);
		}
	}

	// Token: 0x060010B3 RID: 4275 RVA: 0x0007A4CC File Offset: 0x000786CC
	private void Look()
	{
		if (this.target == null || this.target.isDead)
		{
			this.CurrentState = new HeadIK.State(this.Start);
			return;
		}
		this.targetPosition = this.target.transform.position + Vector3.up * this.target.Height * 0.9f;
		this.targetWeight = Mathf.Lerp(this.targetWeight, 1f, Time.deltaTime * this.speed);
		if (!GlobalPreferences.LookAtPlayer.value || !this.giantess.canLookAtPlayer)
		{
			this.CurrentState = new HeadIK.State(this.Disabled);
		}
		else if (!this.giantess.senses.CheckVisibility(this.target))
		{
			this.CurrentState = new HeadIK.State(this.CantSee);
		}
	}

	// Token: 0x060010B4 RID: 4276 RVA: 0x0007A5CC File Offset: 0x000787CC
	private void LookPoint()
	{
		this.targetWeight = Mathf.Lerp(this.targetWeight, 1f, Time.deltaTime * this.speed);
		this.targetPosition = this.lookDirection;
		if (!GlobalPreferences.LookAtPlayer.value || !this.giantess.canLookAtPlayer)
		{
			this.CurrentState = new HeadIK.State(this.Disabled);
		}
	}

	// Token: 0x060010B5 RID: 4277 RVA: 0x0007A638 File Offset: 0x00078838
	private void Disabled()
	{
		this.targetWeight = Mathf.Lerp(this.targetWeight, 0f, Time.deltaTime * this.speed);
		if (GlobalPreferences.LookAtPlayer.value && this.giantess.canLookAtPlayer)
		{
			this.CurrentState = new HeadIK.State(this.Start);
		}
	}

	// Token: 0x060010B6 RID: 4278 RVA: 0x0007A698 File Offset: 0x00078898
	private void TotalDisable()
	{
		this.targetWeight = Mathf.Lerp(this.targetWeight, 0f, Time.deltaTime * this.speed);
	}

	// Token: 0x04001569 RID: 5481
	private LookAtIK ik;

	// Token: 0x0400156A RID: 5482
	private Giantess giantess;

	// Token: 0x0400156B RID: 5483
	private EntityBase target;

	// Token: 0x0400156C RID: 5484
	private Vector3 lookDirection;

	// Token: 0x0400156D RID: 5485
	private Transform head;

	// Token: 0x0400156E RID: 5486
	private Transform neck;

	// Token: 0x0400156F RID: 5487
	private float speed;

	// Token: 0x04001570 RID: 5488
	private float bodyWeight = 0.2f;

	// Token: 0x04001571 RID: 5489
	private const float bodyWeightStand = 0.2f;

	// Token: 0x04001572 RID: 5490
	private const float bodyWeightBend = 0.75f;

	// Token: 0x04001573 RID: 5491
	private const float headWeight = 1f;

	// Token: 0x04001574 RID: 5492
	private float bodyDistance = 1000f;

	// Token: 0x04001575 RID: 5493
	private float headDistance = 400f;

	// Token: 0x04001576 RID: 5494
	private float targetWeight;

	// Token: 0x04001577 RID: 5495
	public bool unlockValues;

	// Token: 0x04001578 RID: 5496
	private Vector3 virtualTargetPosition;

	// Token: 0x04001579 RID: 5497
	private Vector3 virtualIKPosition;

	// Token: 0x0400157A RID: 5498
	private HeadIK.State CurrentState;

	// Token: 0x020002CB RID: 715
	// (Invoke) Token: 0x060010B8 RID: 4280
	private delegate void State();
}
